//
// AWS Manager - A free AWS console application demonstrating advanced concepts
// Copyright (C) 2008-2010 Sean O'Dell. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.awsmanager.common;

import com.xerox.amazonws.sdb.SDBException;
import com.xerox.amazonws.sdb.SimpleDB;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sean ODell
 */
public class SDBConnection {

    private SimpleDB simpleDB = null;
    private String sdbdomain = null;
    private String statusmsg = null;

    public SDBConnection(Properties awsProps, Map sdbServerInfo) {
        connect(awsProps, sdbServerInfo);
    }

    public SimpleDB getsimpleDB() {
        return simpleDB;
    }

    public String getsdbdomain() {
        return sdbdomain;
    }

    public String getstatusmsg() {
        return statusmsg;
    }

    public void connect(Properties awsProps, Map sdbServerInfo) {

        if ((awsProps.getProperty("aws.sdbdomain") != null)
                && (!awsProps.getProperty("aws.sdbdomain").equals(""))) {

            sdbdomain = awsProps.getProperty("aws.sdbdomain");
            SDBServer sdbServer = (SDBServer) sdbServerInfo.get(awsProps.getProperty("aws.sdbserver"));

            if (sdbServer.getServerPort() > 0) {
                simpleDB = new SimpleDB(awsProps.getProperty("aws.accessKey"),
                        awsProps.getProperty("aws.secretKey"), true, sdbServer.getServer(), sdbServer.getServerPort());
            } else {
                simpleDB = new SimpleDB(awsProps.getProperty("aws.accessKey"),
                        awsProps.getProperty("aws.secretKey"), true, sdbServer.getServer());
            }

            if (sdbServer.getServerProxyHost() != null) {
                simpleDB.setProxyValues(sdbServer.getServerProxyHost(), sdbServer.getServerProxyPort());
            }

            try {
                if (simpleDB != null) {
                    simpleDB.listDomains();
                }
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(SDBConnection.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SDBException ex) {
                statusmsg = "SDB " + ex.getMessage();
                Logger.getLogger(SDBConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            simpleDB = null;
            statusmsg = "SDB domain not set. Object meta data functionality disabled.";
        }
    }
}
